fix: support valid JSON value in tool structured content output#551
Merged
tzolov merged 1 commit intomodelcontextprotocol:mainfrom Sep 15, 2025
Merged
fix: support valid JSON value in tool structured content output#551tzolov merged 1 commit intomodelcontextprotocol:mainfrom
tzolov merged 1 commit intomodelcontextprotocol:mainfrom
Conversation
- Change CallToolResult.structuredContent type from Map<String,Object> to Object to support both objects and arrays - Update JsonSchemaValidator to validate any Object type, not just Maps - Add test cases for array-type structured output validation - Fix type casting in existing tests to handle the more generic Object type - Deprecate CallToolResult constructors in favor of builder pattern - Update server implementations to use CallToolResult builder This allows tools to return arrays as structured content, not just objects, which is required by the MCP specification for tools with array-type output schemas. It is realated to MPC spec fix: modelcontextprotocol/modelcontextprotocol#834 Fixes modelcontextprotocol#550 BREAKING CHANGE! Migration Guide: ---------------- 1. If you're accessing CallToolResult.structuredContent(): - Add explicit casting when you know it's a Map: Before: result.structuredContent().get("key") After: ((Map<String,Object>) result.structuredContent()).get("key") - Or check the type first: if (result.structuredContent() instanceof Map) { Map<String,Object> map = (Map<String,Object>) result.structuredContent(); // use map } else if (result.structuredContent() instanceof List) { List<?> list = (List<?>) result.structuredContent(); // use list } 2. If you're creating CallToolResult instances: - Switch to using the builder pattern: Before: new CallToolResult(content, isError, structuredContent) After: CallToolResult.builder() .content(content) .isError(isError) .structuredContent(structuredContent) .build() 3. If you're implementing JsonSchemaValidator: - Update your validate() method signature: Before: validate(Map<String,Object> schema, Map<String,Object> structuredContent) After: validate(Map<String,Object> schema, Object structuredContent) Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Contributor
|
@tzolov looks good. The title could be improved. As you state in "Related MCP specification", structured content be any valid JSON, not just arrays (incl: booleans, strings, numbers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This allows tools to return arrays as structured content, not just objects, which is required by the MCP specification for tools with array-type output schemas.
It is related to MPC spec fix: modelcontextprotocol/modelcontextprotocol#834
Fixes #550
This PR extends the MCP Java SDK to support arrays as structured content output from tools, not just objects. The main change is updating
CallToolResult.structuredContentfromMap<String,Object>toObjectto accommodate both object and array return types.Motivation and Context
This change addresses issue #550. Previously, the SDK only supported tools that returned objects as structured content, but the MCP specification allows tools to define output schemas that return arrays. This limitation prevented developers from creating tools that return lists of items (e.g., a tool that returns a list of users, search results, or any collection of structured data).
Without this change, tools with array-type output schemas would fail validation or cause runtime errors when trying to return array data.
How Has This Been Tested?
testStructuredOutputOfObjectArrayValidationSuccessin multiple test classes to verify array supportBreaking Changes
Yes, this is a breaking change. Users will need to update their code when:
Accessing
CallToolResult.structuredContent(): Add explicit casting since it now returnsObjectinstead ofMap<String,Object>Creating
CallToolResultinstances: The constructors are deprecated, use the builder patternImplementing custom
JsonSchemaValidator: Update the validate method signatureTypes of changes
Checklist
Additional context
Design Decisions:
structuredContenttoObjecttype to support both Maps and Lists, aligning with JSON's two container typesCallToolResultto provide a cleaner API and better forward compatibility@Deprecated)Migration Path:
The deprecated constructors are retained for backward compatibility but should be removed in a future major version. Users are encouraged to migrate to the builder pattern immediately.
Related MCP Specification:
This change aligns with the MCP Tools Specification which states that structured content can be any valid JSON value, including arrays.
modelcontextprotocol/modelcontextprotocol#834